/******提供通知的脚本
 //1、打印
 // 2、页面刷新
 //3、导出Excel
 //4、其它
*/

 /*************
//Function: afford general print support  
//Parameter:
//Description:包括打印预览、直接打印和打印设定、打印控件生成。
*/

function createPrintWB()
{
	// if not exist the WebBrowse Object, then Create it.
	if (!document.printWB)  
	{  
		document.body.insertAdjacentHTML("beforeEnd",
		"<object id=\"printWB\" width=0 height=0 \
		classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");
	}
} //createPrintWB
 
function printPreview() 
{ //Print preview
	createPrintWB();
	document.focus();
	//OLECMDID_PRINTPREVIEW = 7, OLECMDEXECOPT_PROMPTUSER     = 1,
	// OLECMDID_PRINT       = 6, LECMDEXECOPT_DONTPROMPTUSER  = 2, 
	// OLECMDID_PAGESETUP   = 8, OLECMDEXECOPT_PROMPTUSER     = 1,
	// 
	//
	//OLECMDEXECOPT_SHOWHELP         = 3 
	eval('document.printWB.Execwb(7,1)');  
}  //printPreview

function printDirect() 
{ //print Directly 
	createPrintWB();
	document.focus();
	// OLECMDID_PRINT       = 6, LECMDEXECOPT_DONTPROMPTUSER  = 6, 
	//eval('document.printWB.Execwb(6,6)');     
	eval('document.printWB.Execwb(7,1)');  
} //printDirect

function printSet() 
{ //print page setup
	createPrintWB();
	document.focus();
	// OLECMDID_PAGESETUP   = 8, OLECMDEXECOPT_PROMPTUSER     = 1,
	eval('document.printWB.Execwb(8,1)');     
} //printSet

  		
/***********************
// function:提示页面正在更新网页
//   1、有提示字样。 2。有光标正在等待的出现 
*/
function inUpdate()
{ 
 // var strHTML;
  //strHTML="<p align=center><font color='#FF0000'>系统正在进行更新页面，请稍候...</font></p>";
  //document.body.style.cursor="wait";
  /*strHTML="<table width='200' height='50' border='1' cellpadding='0' cellspacing='0' bordercolor="#FF6600'> "
		 + "<tr><td><table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0' bgcolor='#999999' > "
		 + "<tr> <td>&nbsp;</td></tr><tr> <td align='center'<font color='#FF0000'>正在更新，请稍候...</font></td></tr>"
		 + "<tr> <td>&nbsp;</td></tr></table></td></tr></table>";
  */
 // document.body.insertAdjacentHTML("afterBegin",strHTML);
} //inUpdate


/*************************************
//Function: transfer the data from web table  to excel
//Input parameter:string   strCols    可为数组列，要求显示声明为字符的栏位。默认为无。    
//              int      colsBegin  导出起始列.默认为0 
//              int      colsEnd    导出终止列，默认为自行判断。
//              int      rowsBegin  导出起始行，默认为自行判断。
//              int      rowsEnd    导出终止行，默认为自行判断。 
*/

function automateExcel(strCols,colsBegin,colsEnd,rowsBegin,rowsEnd)  
{ 
	// Start Excel and get Application object.
	var oXL = new ActiveXObject("Excel.Application"); 
	// Get a new workbook.
	var oWB = oXL.Workbooks.Add();
	var oSheet = oWB.ActiveSheet;
	//Detection the table(ListDataGrid) ,rows' and cols' count	
	var table = document.all.ListDataGrid; 
	var rows = table.rows.length;   
	var cols;
	var rowsLength,colsLength;
	var isTitle;
	var strInnerText,strInnerHTML,ifAdd;
	var i=0,j=0;
	//注意：表格组数从0开始，但行数、列数是从1开始计。故对应的行、列要减1
	//读取数据需要的参数：总行数rows,数据读取起始行rowsBegin，数据读取行数rowsLength，起始列colsBegin，数据读取列数colsLength		


	//rowsBegin
	if (rowsBegin==null) 
	{  //没有输入值，则需自行判断
		rowsBegin=0;
	}
	else { //有输入值，但不合法。需自行判断
		if ((rowsBegin<0 ) || (rowsBegin>=rows)) 
		{ //不合法
			rowsBegin=0;  
		}
	}
  
  if (rowsBegin==0 ) { //自行判断获取
    //识别表格的表头,即起始行rowsBegin
    //识别机制：InnerText=InnerHTML
    isTitle= false;
	rowsBegin=0
    for (i=0;i<rows;i++)  {
      // window.alert(i)
	  if (isTitle==true) {
	     break;
	   }
	  cols=table.rows(i).cells.length;
	  isTitle=false;
	  //当前rows的所有cols均是文字时，即为表头	  
	  //rowsBegin  
	  for (j=0;j<cols;j++) {
	    //remove the &nbsp and space before comparation
        strInnerText=table.rows(i).cells(j).innerText;
        strInnerHTML=table.rows(i).cells(j).innerHTML;
        
        strInnerText=strInnerText.replace(" ","");
        strInnerHTML=strInnerHTML.replace("&nbsp;"," ");
        strInnerHTML=strInnerHTML.replace(" ","");
        
        if (strInnerText==strInnerHTML) {
		  isTitle=true;		  
		 }
	   }
	  rowsBegin=i			//不理解
     }  
    if ( (rowsBegin>rows) || (rowsBegin<=0) ) {  rowsBegin=rows; }  //防止overflow 是多的
     
   } //(rowsBegin==0 ) 
  
   
//colsBegin
  cols=table.rows(rowsBegin-1).cells.length;
  if (colsBegin==null) {  //没有输入值，则从首列1开始
     colsBegin=1;
   }
  else { //有输入值，但不合法。则从首列1开始
    if ((colsBegin<=0 ) && (colsBegin>=cols)) { //不合法
      colsBegin=1;  
     }
   }
  
//colsEnd
  cols=table.rows(rowsBegin-1).cells.length;
  if (colsEnd==null) {  //没有输入值，则需自行判断
     colsEnd=0;
   }
  else { //有输入值，但不合法。则需自行判断
    if ((colsEnd<0 ) && (colsEnd>cols)) { //不合法
      colsEnd=0;  
     }
   }
  if ( colsEnd==0) {
    //识别内容的cols列数，判断表头的下一列情况，即数到按钮前一列止。
    
    var re;
    for (j=0;j<cols;j++) {
      //remove the &nbsp and space before comparation
      strInnerText=table.rows(i).cells(j).innerText;
      strInnerHTML=table.rows(i).cells(j).innerHTML;
          
      re=/&nbsp;/ig;
      strInnerHTML=strInnerHTML.replace(re," ");
      re=/\s/ig; 
      strInnerText=strInnerText.replace(re,"");
      strInnerHTML=strInnerHTML.replace(re,"");
      
      if (strInnerText!=strInnerHTML) {
		colsEnd=j;
		break;
	   }
	 }
	if ( (colsEnd<=0) || (colsEnd>cols) ) { colsEnd=cols; }  //不合法，则取表头列
   }  //( colsEnd==0)
 
 if (colsEnd<colsBegin) { colsEnd=colsBegin; }  //colsBegin<=colsEnd
 
 
 //colsLength
 colsLength=colsEnd-colsBegin+1;
 
//rowsEnd
  if (rowsEnd==null) {  //没有输入值，则需自行判断
     rowsEnd=0;
   }
  else { //有输入值，但不合法。需自行判断
    if ((rowsEnd<0 ) || (rowsEnd>rows)) { //不合法
      rowsEnd=0;  
     }
   }
  
  if (rowsEnd==0 ) { //自行判断获取
    //识别内容的rowsEnd数，判断表头的下一行内容列数变化情况，即数到变化的前一行止。
    cols=table.rows(rowsBegin-1).cells.length;
    if ( rowsBegin==rows ) {   //仅一行表头
      rowsEnd=rows;
     }
    else {
      for (i=rowsBegin;i<rows;i++)  { 
        //列数有变化，数到此为止
        if (cols!=table.rows(i).cells.length) {  break;   }
       }
      rowsEnd=i;
     }
    if ( (rowsEnd<=0) || (rowsEnd>rows) ) { rowsEnd=rows; }  //不合法，则取未行
   } //(rowsEnd==0 )
     
   if (rowsEnd<rowsBegin) { rowsEnd=rowsBegin; }  //rowsBegin<=rowsEnd
   
   //rowsLength
   rowsLength=rowsEnd-rowsBegin+1;
 
     
   //从传入参数中获取需要显示说明为字符串的栏位。
      var colArray = new Array(colsLength);
      var colInArray= new Array();
      
      for (i=0;i<colsLength;i++) {
        colArray[i]=0;
       }
      
      if (strCols==null) { strCols="";}
      
      if (strCols.length>=1 && (strCols.indexOf(",")<0) ) {
         if (parseInt(strCols)<=colsLength && (parseInt(strCols)>0) ) { //输入参数合法
           i=parseInt(strCols); 
           colArray[i-1]=i;
          }
       }   
      else {
        if (strCols.length>2 && (strCols.indexOf(",")>0) ) {
          colInArray=strCols.split(",");
          
          for (i=0;i<colInArray.length;i++) {
            if (parseInt(colInArray[i])<=colsLength && parseInt(colInArray[i])>0) { //输入参数合法
              j=parseInt(colInArray[i]); 
              colArray[j-1]=j;
             }
           }          
         }
       }  
       

            
     // transfer the table data to Excel 
     
     //注意：Excel中columns下标从1开始,故下标值与table相差2
      ifAdd=""
      for (i=1;i<=rowsLength;i++) {
	    for (j=1;j<=colsLength;j++) {
          if (colArray[j-1]==j) { ifAdd="'"; }  //将数字字符加"'",让Excel识别
          if ((rowsBegin-2+i)>=rows) { break; } //防溢出 
          if ( (colsBegin-2+j)>=(table.rows(rowsBegin-2+i).cells.length)){ break;} //防溢出
	      strInnerText=ifAdd + table.rows(rowsBegin-2+i).cells(colsBegin-2+j).innerText;

	      if (i==1 ) //去除标题行中的↑，↓
	      {
			strInnerText =  strInnerText.replace("↑","");
			strInnerText =  strInnerText.replace("↓","");
	      }
     
		  oSheet.Cells(i,j).Value =strInnerText ;
		  ifAdd="";
		 }//for
       }//for

	// for Excel columns name and style
      var X
	  switch(j-1){
	         case 1: X = "A";break;
             case 2: X = "B";break;
             case 3: X = "C";break;
             case 4: X = "D";break;
             case 5: X = "E";break;
             case 6: X = "F";break;
             case 7: X = "G";break;
             case 8: X = "H";break;
             case 9: X = "I";break;
             case 10: X = "J";break;
             case 11: X = "K";break;
             case 12: X = "L";break;
             case 13: X = "M";break;
             case 14: X = "N";break;
             case 15: X = "O";break;
             case 16: X = "P";break;
             case 17: X = "Q";break;
             case 18: X = "R";break;
             case 19: X = "S";break;
             case 20: X = "T";break;
             case 21: X = "U";break;
             case 22: X = "V";break;
             case 23: X = "W";break;
             case 24: X = "X";break;
             case 25: X = "Y";break;
             case 26: X = "Z";break;
             case 27: X = "AA";break;
             case 28: X = "AB";break;
             case 29: X = "AC";break;
             case 30: X = "AD";break;
             case 31: X = "AE";break;
             case 32: X = "AF";break;
             case 33: X = "AG";break;
             case 34: X = "AH";break;
             case 35: X = "AI";break;
             case 36: X = "AJ";break;
             case 37: X = "AK";break;
             case 38: X = "AL";break;
             case 39: X = "AM";break;
             case 40: X = "AN";break;
             case 41: X = "AO";break;
             case 42: X = "AP";break;
             case 43: X = "AQ";break;
             case 44: X = "AR";break;
             case 45: X = "AS";break;
             case 46: X = "AT";break;
             case 47: X = "AU";break;
             case 48: X = "AV";break;
             case 49: X = "AW";break;
             case 50: X = "AX";break;
             case 51: X = "AY";break;
             case 52: X = "AZ";break;
	  }
	  //set columns style
			var U="A:"+X ;                     //Excel table columns
			var H= "A1:"+X+"1" ;                 //Excel table head
      var X = "A1:"  + X + rowsLength;       //Excel table all
      var xlBorderLineStyleContinuous;
	  // oXL.ActiveSheet.Range(H).Font.ColorIndex = 3;       
      oXL.ActiveSheet.Range(H).Font.Bold = true;
	  oXL.ActiveSheet.Range(H).HorizontalAlignment  =3;
      oXL.ActiveSheet.Range(X).Borders.LineStyle = xlBorderLineStyleContinuous;
      oXL.ActiveSheet.Range(X).Borders.ColorIndex = 1;  


	  oXL.ActiveSheet.Columns(U).autofit
	  oXL.Visible = true;
	  oXL.UserControl = true;

	  
	 /*// save the Excel document
	 var filename=document.frmfind.upfilename.value;//"f:\\new.xls";
	 if (filename=="")  {
	   filename="d:\\new.xls";
	  }
	 oSheet.SaveAs (filename);
	 */
	 oSheet.Application.Quit();
	
	 oSheet=null;
	 oWB=null; 
	 oXL ==null;
	 delete oSheet;
	 delete oWB;
	 delete oXL;
	// idTmr = window.setInterval("Cleanup();",1);
	
	 
}  //automateExcel

// recycle the memory
function cleanup() 
{  
    window.clearInterval(idTmr);
    CollectGarbage();
} // //Cleanup
